using System.Linq;
using System.Threading.Tasks;
using DailyPoetryX.AzureStorage.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace DailyPoetryX.AzureStorage {
    /// <summary>
    /// Ping。
    /// </summary>
    public class Ping {
        /// <summary>
        /// 身份验证服务。
        /// </summary>
        private static IAuthenticationService _authenticationService;

        /// <summary>
        /// 授权服务。
        /// </summary>
        private static IAuthorizationService _authorizationService;

        /// <summary>
        /// Ping。
        /// </summary>
        /// <param name="authenticationService">身份验证服务。</param>
        /// <param name="authorizationService">授权服务。</param>
        public Ping(IAuthenticationService authenticationService,
            IAuthorizationService authorizationService) {
            _authenticationService = authenticationService;
            _authorizationService = authorizationService;
        }

        [FunctionName("Ping")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]
            HttpRequest req, ILogger log) {
            log.LogInformation(
                "Ping HTTP trigger function processed a request.");

            var authorizationHelperResult =
                await AuthorizationHelper.Authorize(req, log,
                    _authenticationService, _authorizationService);

            return authorizationHelperResult.Passed
                ? new OkObjectResult("Pong")
                : authorizationHelperResult.ActionResult;
        }
    }
}